package Controller;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Time;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import Dao.ArrangeInfoDao;
import Model.*;
import Service.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.aspectj.lang.ProceedingJoinPoint;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import utils.permission.ConstantsUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Controller
@RequestMapping("/arrangeInfo")
public class ArrangeInfoController {
    private static final Logger logger = LoggerFactory.getLogger(ArrangeInfoController.class);
    @Autowired
    private ArrangeInfoService arrangeInfoService;


    @Autowired
    private ElderInfoService elderInfoService;
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private UserService userService;
    @Autowired
    private StaffService staffService;

    @Autowired
    ArrangeInfoDao arrangeInfoDao;

    public ArrangeInfoController(ElderInfoService elderInfoService) {
        this.elderInfoService = elderInfoService;
    }



    @RequestMapping("/saveImage")
    @ResponseBody
    public String saveImaga(MultipartFile file, HttpServletRequest request) throws IOException {
        System.out.println("filePath:::11111");
        //String name = UUID.randomUUID().toString().replaceAll("-", "");
        //设置图片上传路径,是目标文件夹的路径
        String filePath = "D:\\Image\\image";
        System.out.println("filePath:::"+filePath);
        // 获取原始图片的扩展名
        String originalFilename = file.getOriginalFilename();
        System.out.println("originalFilename:::"+originalFilename);

        // 生成文件新的名字
        String newFileName = UUID.randomUUID().toString().replaceAll("-", "") + originalFilename;
        System.out.println("newFileName:::"+newFileName);

        // 封装上传文件位置的全路径
        File targetFile = new File(filePath, newFileName);
        file.transferTo(targetFile);

        // 保存到数据库
        ArrangeInfo product=new ArrangeInfo();
        product.setPimage(newFileName);
        System.out.println("11111111:::"+product.getPimage());
        arrangeInfoDao.saveimage(product);
        System.out.println("222222:::"+product.getPimage());
        //重定向到查看图片的方法上去
        return "{\"msg\":\"ok\"}";

    }

    @RequestMapping("/showimage")
    @ResponseBody
    public List showimage( HttpServletRequest request, HttpServletResponse response, HttpSession session){
        System.out.println("来到显示图片");
        List<ArrangeInfo> list= arrangeInfoDao.showimage();
        System.out.println("list:"+list);
        return list;
    }


    /**
     * 跳转到"新增派工订单"页面
     */
    @RequestMapping(value="toAddArrangeInfo")
    public String AddArrangeInfo(HttpServletRequest request, HttpServletResponse response, HttpSession session){
        System.out.println("跳转到新增派工信息界面");
        return "/SmartCheck/ArrangeInfo/addArrangeInfo";
    }

    /**
     * 跳转到"派工日程安排"页面
     */
    @RequestMapping(value="toDateArrangeInfo")
    public String DateArrangeInfo(HttpServletRequest request, HttpServletResponse response, HttpSession session){
        System.out.println("跳转到新增派工信息界面");
        return "/SmartCheck/ArrangeInfo/dateArrangeInfo";
    }

    /**
     * 跳转到"派工日程安排(护工时间)"页面
     */
    @RequestMapping(value="showDateArrange")
    public String showDateArrange(HttpServletRequest request, HttpServletResponse response, HttpSession session){
        System.out.println("跳转到新增派工信息界面");
        return "/SmartCheck/ArrangeInfo/showDateArrange";
    }

    /**
     * 派工单信息页面：显示所有派工信息（表格形式）
     * @return
     */
    @RequestMapping("/showArrangeInfo")
    @ResponseBody
    public PageInfo getAllArrangeInfoList(HttpServletRequest request,@RequestParam(value = "pn", defaultValue = "1") Integer pn, @RequestParam(value = "limit", defaultValue = "10") Integer limit) throws ParseException {
        User user = (User) request.getSession().getAttribute(ConstantsUtils.SessionObj.CURRENT_USER);
        String Role_id=this.userService.findRoleIDbyUserNumber(user.getUser_number());
        List<ArrangeInfo> arrangeInfoList = arrangeInfoService.getAllArrangeInfoList();
        SimpleDateFormat formatter= new SimpleDateFormat("yyyyMMdd");
        Date date = new Date(System.currentTimeMillis());
        String current=formatter.format(date);

        System.out.println("开始时间："+current);

        Date st = formatter.parse(current);
        System.out.println("开始时间1："+current);
        for(int i=0;i<arrangeInfoList.size();i++){
            System.out.println("开始时间2："+current);
            ArrangeInfo arrangeInfo=arrangeInfoList.get(i);
            System.out.println("开始时间3："+current);
            if(arrangeInfo.getServ_due_date()==null){
                System.out.println("开始时间2222："+current);
                continue;
            }
            else if(arrangeInfo.getServ_due_date().before(st)){
                System.out.println("开始时间4："+current);
                String status="过期";
                int id=arrangeInfo.getId();
                System.out.println("111111:"+status);
                int s=arrangeInfoDao.updateOverTime(id,status);
                System.out.println("111111:"+s);
            }
            else if(st.before(arrangeInfo.getServ_start_date())){
                String status="还未生效";

                System.out.println("222222:"+status);
                int s=arrangeInfoDao.updateOverTime(arrangeInfo.getId(),status);
                System.out.println("222222:"+s);
            }
            else{
                if(arrangeInfo.getStatus()==null||arrangeInfo.getStatus().equals("")){
                    String status="有效";

                    System.out.println("33333:"+status);
                    int s=arrangeInfoDao.updateOverTime(arrangeInfo.getId(),status);
                    System.out.println("33333:"+s);
                }
            }
        }
        List<ArrangeInfo> ShortarrangeInfoList = arrangeInfoDao.getAllShortArrangeInfoList();

        System.out.println("查询是否存在短期派工信息："+ShortarrangeInfoList);
        int[] ShortId=new int[ShortarrangeInfoList.size()];
        for(int i=0;i<ShortarrangeInfoList.size();i++){
            ArrangeInfo ShortarrangeInfo=ShortarrangeInfoList.get(i);
            ShortId[i]=ShortarrangeInfo.getId();
            if(ShortarrangeInfo.getServ_due_date().before(st)){
                ShortId[i]=0;


                System.out.println("短期派工数组："+ShortId[i]);
                String status="过期";
                //int e=arrangeInfoDao.updateshortChange(ShortarrangeInfo.getId(),ShortChange);
                int s=arrangeInfoDao.updateOverShortTime(ShortarrangeInfo.getId(),status);
            }
            else if(st.before(ShortarrangeInfo.getServ_start_date())){
                String status="还未生效";
                System.out.println("短期派工数组1："+status);


                int s=arrangeInfoDao.updateOverShortTime(ShortarrangeInfo.getId(),status);
            }
            else{
                if(ShortarrangeInfo.getStatus()==null||ShortarrangeInfo.getStatus().equals("")){
                    String status="有效";
                    System.out.println("短期派工数组2："+status);
                    int s=arrangeInfoDao.updateOverShortTime(ShortarrangeInfo.getId(),status);
                    System.out.println("成功更新1111");
                    int c=arrangeInfoDao.updateShortChange(ShortarrangeInfo.getId(),status);
                    System.out.println("成功更新22222");
                }
            }
        }

        if(Role_id.equals("2")){
            //      根据user_number找到结算代码（唯一）
            String settleCode =userService.findSettleCodebyUserNumber(user.getUser_number());
            System.out.println("找到结算代码："+settleCode);
            //      根据结算代码找到订单
            PageHelper.startPage(pn, limit);
            List<ArrangeInfo> arrangeInfoList1=arrangeInfoService.getAllArrangeInfobySettleCode(settleCode);

            System.out.println("找到结算代码0："+settleCode);
            List<ArrangeInfo> arrangeShortInfoList=arrangeInfoDao.getAllArrangeShortInfobySettleCode(settleCode);
            System.out.println("找到结算代码1："+arrangeShortInfoList.size());
            System.out.println("找到结算代码1："+settleCode);
            for(int i=0;i<arrangeInfoList1.size();i++){
                System.out.println("找到结算代码0000："+arrangeShortInfoList.size());
                ArrangeInfo arrangeInfoone=arrangeInfoList1.get(i);
                if(arrangeInfoone.getShortChange()==null){
                    System.out.println("找到结算代码2222："+arrangeInfoone.getShortChange());
                }
                else if(arrangeInfoone.getShortChange().equals("有效")||arrangeInfoone.getShortChange().equals("还未生效")){
                    System.out.println("找到结算代码000：");
                    for(int j=0;j<arrangeShortInfoList.size();j++){
                        System.out.println("找到结算代码11111："+arrangeShortInfoList.size());
                        ArrangeInfo arrangeInfoShort=arrangeShortInfoList.get(j);
                        if(arrangeInfoone.getId()==arrangeInfoShort.getId()){
                            if(arrangeInfoShort.getStatus().equals("有效")||arrangeInfoShort.getStatus().equals("还未生效")){
                                System.out.println("找到结算代码3：");
                                arrangeInfoList1.remove(i);
                                System.out.println("找到结算代码4："+arrangeInfoList1.size());
                                arrangeInfoList1.add(i,arrangeInfoShort);
                                System.out.println("找到结算代码5："+arrangeInfoList1.size());
                            }
                        }
                    }
                }
                System.out.println("找到结算代码3："+arrangeInfoList1.size());
            }
            System.out.println("找到结算代码3："+arrangeInfoList1.size());
            PageInfo page=new PageInfo(arrangeInfoList1);
            return page;
        }else{
            PageHelper.startPage(pn, limit);
            List<ArrangeInfo> arrangeInfoList1 = arrangeInfoService.getAllArrangeInfoList();
            PageInfo page=new PageInfo(arrangeInfoList1);
            return page;
        }
    }

    /**
     * 派工单信息页面：派工信息搜索
     * @param servOrg
     * @param settleCode
     * @param servStatus
     * @param elderName
     * @param elderYbid
     * @param elderId
     * @param nursingName
     * @param nursingYbid
     * @param nursingId
     * @param startDate
     * @param dueDate
     * @param startTime
     * @return
     */
    @RequestMapping("/searchArrangeInfo")
    @ResponseBody
    public PageInfo searchArrangeInfo(HttpServletRequest request,@RequestParam(value = "pn", defaultValue = "1") Integer pn,
                                      @RequestParam(value = "limit", defaultValue = "10") Integer limit, @RequestParam(value = "servOrg") String servOrg , @RequestParam(value="settleCode")String settleCode,
                                      @RequestParam(value="servStatus")String servStatus, @RequestParam(value = "elderName") String elderName ,
                                      @RequestParam(value="elderYbid")String elderYbid, @RequestParam(value="elderId")String elderId,
                                      @RequestParam(value = "nursingName") String nursingName , @RequestParam(value="nursingYbid")String nursingYbid,
                                      @RequestParam(value="nursingId")String nursingId, @RequestParam(value = "startDate") String startDate ,
                                      @RequestParam(value="dueDate")String dueDate, @RequestParam(value="startTime")String startTime)
    {
        User user = (User) request.getSession().getAttribute(ConstantsUtils.SessionObj.CURRENT_USER);
        String Role_id=this.userService.findRoleIDbyUserNumber(user.getUser_number());
        if (settleCode == "" && servOrg =="" && servStatus ==""&&elderName == "" && elderYbid =="" && elderId ==""
                && nursingName == "" && nursingYbid =="" && nursingId ==""&& startDate == "" && dueDate =="" && startTime =="") {
            System.out.println("搜索框无数据");
            if(Role_id.equals("2")){
                String thisSettleCode =userService.findSettleCodebyUserNumber(user.getUser_number());
                PageHelper.startPage(pn, limit);
                List<ArrangeInfo> arrangeInfos=arrangeInfoService.getAllArrangeInfobySettleCode(thisSettleCode);
                PageInfo page=new PageInfo(arrangeInfos);
                return page;
            }else{
                PageHelper.startPage(pn, limit);
                List<ArrangeInfo> arrangeInfos = arrangeInfoService.getAllArrangeInfoList();
                PageInfo page = new PageInfo(arrangeInfos);
                return page;
            }
        }
        else{
            System.out.println("contoller层：搜索框非空");
            if(Role_id.equals("2")){
                String thisSettleCode =userService.findSettleCodebyUserNumber(user.getUser_number());
                System.out.println("contoller层："+servStatus);
                PageHelper.startPage(pn, limit);
                List<ArrangeInfo> arrangeInfos = arrangeInfoService.findArrangeInfobySettleCode(thisSettleCode,servOrg,servStatus,elderName,
                        elderYbid,elderId,nursingName,nursingYbid,
                        nursingId,startDate,dueDate,startTime);
                PageInfo page = new PageInfo(arrangeInfos);
                return page;
            }else{
                System.out.println("contoller层1："+servStatus);
                PageHelper.startPage(pn, limit);
                List<ArrangeInfo> arrangeInfos = arrangeInfoService.findArrangeInfo(servOrg,settleCode,servStatus,elderName,
                        elderYbid,elderId,nursingName,nursingYbid,
                        nursingId,startDate,dueDate,startTime);
                PageInfo page = new PageInfo(arrangeInfos);
                return page;
            }
        }
    }

    /**
     * 上传Excel获取机构数据
     */
    @RequestMapping(value = "/InputExcel")
    @ResponseBody
    public String InputExcel(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws Exception {
        String flag = "02";// 上传标志
        if (!file.isEmpty()) {
            try {
                User user = (User) request.getSession().getAttribute(ConstantsUtils.SessionObj.CURRENT_USER);
                String settleCode =userService.findSettleCodebyUserNumber(user.getUser_number());
                String originalFilename = file.getOriginalFilename();// 原文件名字
                System.out.println("文件名：" + originalFilename);
                InputStream is = file.getInputStream();// 获取输入流
                flag = arrangeInfoService.InputExcel(is, originalFilename,settleCode);
                System.out.println("controller层：上传成功，flag= "+flag);
                return "{\"msg\":\"ok\"}";
            } catch (Exception e) {
                flag = "03";// 上传出错
                e.printStackTrace();
                System.out.println("controller层：上传失败，flag= "+flag);
            }
        }
        return "{\"msg\":\"error\"}";
//        return flag;
    }


    /**
     * 派工单信息页面：导出信息
     * @param servOrg
     * @param settleCode
     * @param servStatus
     * @param elderName
     * @param elderYbid
     * @param elderId
     * @param nursingName
     * @param nursingYbid
     * @param nursingId
     * @param startDate
     * @param dueDate
     * @param startTime
     * @return
     */
    @RequestMapping("/ExportArrangeInfo")
    @ResponseBody
    public List<ArrangeInfo> ExportArrangeInfo(HttpServletRequest request,@RequestParam(value = "servOrg") String servOrg , @RequestParam(value="settleCode")String settleCode,
                                      @RequestParam(value="servStatus")String servStatus, @RequestParam(value = "elderName") String elderName ,
                                      @RequestParam(value="elderYbid")String elderYbid, @RequestParam(value="elderId")String elderId,
                                      @RequestParam(value = "nursingName") String nursingName , @RequestParam(value="nursingYbid")String nursingYbid,
                                      @RequestParam(value="nursingId")String nursingId, @RequestParam(value = "startDate") String startDate ,
                                      @RequestParam(value="dueDate")String dueDate, @RequestParam(value="startTime")String startTime)
    {
        User user = (User) request.getSession().getAttribute(ConstantsUtils.SessionObj.CURRENT_USER);
        String Role_id=this.userService.findRoleIDbyUserNumber(user.getUser_number());
        if (settleCode == "" && servOrg =="" && servStatus ==""&&elderName == "" && elderYbid =="" && elderId ==""
                && nursingName == "" && nursingYbid =="" && nursingId ==""&& startDate == "" && dueDate =="" && startTime =="") {
            System.out.println("搜索框无数据");
            if(Role_id.equals("2")){
                String thisSettleCode =userService.findSettleCodebyUserNumber(user.getUser_number());
                List<ArrangeInfo> arrangeInfos=arrangeInfoService.getAllArrangeInfobySettleCode(thisSettleCode);
                return arrangeInfos;
            }else{
                List<ArrangeInfo> arrangeInfos = arrangeInfoService.getAllArrangeInfoList();
                return arrangeInfos;
            }
        }
        else{
            System.out.println("contoller层：搜索框非空");
            if(Role_id.equals("2")){
                String thisSettleCode =userService.findSettleCodebyUserNumber(user.getUser_number());
                List<ArrangeInfo> arrangeInfos = arrangeInfoService.findArrangeInfobySettleCode(thisSettleCode,servOrg,servStatus,elderName,
                        elderYbid,elderId,nursingName,nursingYbid,
                        nursingId,startDate,dueDate,startTime);
                return arrangeInfos;
            }else{
                List<ArrangeInfo> arrangeInfos = arrangeInfoService.findArrangeInfo(servOrg,settleCode,servStatus,elderName,
                        elderYbid,elderId,nursingName,nursingYbid,
                        nursingId,startDate,dueDate,startTime);
                return arrangeInfos;
            }
        }
    }
    /**
     * 新增订单页面：搜索要被派工的老人
     * @param elderName
     * @param Ybid
     * @param ID
     * @return
     */
    @RequestMapping("/searchArrangeElder")
    @ResponseBody
    public PageInfo searchArrangeElder(HttpServletRequest request,@RequestParam(value = "pn", defaultValue = "1") Integer pn,
                                       @RequestParam(value = "limit", defaultValue = "10") Integer limit,
                                       @RequestParam(value = "elderName") String elderName , @RequestParam(value="Ybid")String Ybid,
                                       @RequestParam(value="ID")String ID) {
        User user = (User) request.getSession().getAttribute(ConstantsUtils.SessionObj.CURRENT_USER);
        String Role_id=this.userService.findRoleIDbyUserNumber(user.getUser_number());
        if (elderName == "" && Ybid =="" && ID=="") {
            System.out.println("搜索框无数据");
            if(Role_id.equals("2")){
                String SettleCode =userService.findSettleCodebyUserNumber(user.getUser_number());
                PageHelper.startPage(pn, limit);
                List<ElderInfo> elderInfos = elderInfoService.getElderbySettleCode(SettleCode);
                PageInfo page = new PageInfo(elderInfos);
                return page;
            }
            else{
                PageHelper.startPage(pn, limit);
                List<ElderInfo> elderInfos=elderInfoService.getAllElderList();
                PageInfo page = new PageInfo(elderInfos);
                return page;
            }
        } else{
            if(Role_id.equals("2")){
                String SettleCode =userService.findSettleCodebyUserNumber(user.getUser_number());
                List<ElderInfo> elderInfos = elderInfoService.searchArrangeElderbySettleCode(elderName,Ybid,ID,SettleCode);
                PageInfo page = new PageInfo(elderInfos);
                return page;
            }
            else{
                List<ElderInfo> elderInfos = elderInfoService.searchArrangeElder(elderName,Ybid,ID);
                PageInfo page = new PageInfo(elderInfos);
                return page;
            }
        }
    }

    /**
     * 新增订单页面：获取当前登录的机构信息（根据登录信息）
     */
    @RequestMapping("/getArrangeOrg")
    @ResponseBody
    public List<Department> getArrangeOrg(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
        System.out.println("进入controller层");
        User user = (User) request.getSession().getAttribute(ConstantsUtils.SessionObj.CURRENT_USER);
        System.out.println("controller层：user信息:" + user);
        System.out.println("controller层：user_number:" + user.getUser_number());
//      根据user_number找到结算代码（唯一）
        String settleCode = userService.findSettleCodebyUserNumber(user.getUser_number());
        System.out.println("找到结算代码："+settleCode);
//      根据结算代码找到服务机构
        List<Department> departmentInfo=departmentService.findOrgInfobySettleCode(settleCode);
        return departmentInfo;
    }

    /**
     * 新增订单页面：新增订单提交
     * @param elderYbid
     * @param OrgSettleCode
     * @param servContent
     * @param servID
     * @return
     */
    @RequestMapping("/newOrder")
    @ResponseBody
    public String newOrder(@RequestParam(value = "elderYbid") String elderYbid , @RequestParam(value="OrgSettleCode")String OrgSettleCode,
                           @RequestParam(value="servContent") List<String> servContent,@RequestParam(value="servID")List<String>servID){
        System.out.println("输出老人医保ID："+elderYbid);
        System.out.println("输出机构结算代码："+OrgSettleCode);
        System.out.println("输出服务内容："+servContent);
        System.out.println("输出服务ID："+servID);
        try{
            if(elderYbid!=""&&OrgSettleCode!=""){
                String arrangeInfos=arrangeInfoService.insertArrangeInfo(elderYbid,OrgSettleCode,servContent,servID);
                System.out.println(arrangeInfos);
            }
            else{
                System.out.println("存在空参数");
                return "{\"msg\":\"error\"}";
            }
        }
        catch (Exception e) {
            e.printStackTrace();
            logger.error("更新异常！", e);
            return "操作异常，请您稍后再试";
        }
        return "{\"msg\":\"ok\"}";
    }

    /**
     * 派工日程安排页面：根据老人医保ID找到服务老人
     * @param elderYbid
     * @return
     */
    @RequestMapping("/findElderInfobyYbid")
    @ResponseBody
    public List<ElderInfo> findElderInfobyYbid(@RequestParam(value = "elderYbid") String elderYbid){
        System.out.println("进入controller层，输出老人医保ID："+elderYbid);
        List<ElderInfo> elderInfos = elderInfoService.findElderInfobyYbid(elderYbid);
        System.out.println("controller层，输出老人信息："+elderInfos);
        return elderInfos;
    }
    /**
     * 派工日程安排页面：一个老人被服务时间显示（表格）
     * @return
     * @param ArrangeID
     */
    @RequestMapping("/showDateArrangeInfo")
    @ResponseBody
    public PageInfo showDateArrangeInfo(@RequestParam(value="ArrangeID")String ArrangeID,@RequestParam(value = "pn", defaultValue = "1") Integer pn, @RequestParam(value = "limit", defaultValue = "1") Integer limit) {
        PageHelper.startPage(pn, limit);
        if(ArrangeID==""||ArrangeID==null){
            PageInfo page = new PageInfo();
            return page;
        } else{
            String findChosenDaybyID=arrangeInfoService.findChosenDaybyID(ArrangeID);
            if(findChosenDaybyID==null||findChosenDaybyID==""){
                System.out.println("controller层：此ID下并未选择工作日");
                PageInfo page = new PageInfo();
                return page;
            }else{
                System.out.println("controller层：此ID下已有工作日");
                List<ArrangeInfo> dateArrangeInfos = arrangeInfoService.getDateArrangeInfo(ArrangeID);
                PageInfo page = new PageInfo(dateArrangeInfos);
                return page;
            }
        }
    }
    /**
     * 派工日程安排页面：确定服务时间
     * @param StartDate
     * @param SelectTime
     * @param StartTime
     * @param ServWorkLength
     * @param StaffYbid
     * @param ChosenWeekday
     * @param ArrangeID
     * @return
     */
    @RequestMapping("/choseServTime")
    @ResponseBody
    public String choseServTime(@RequestParam(value="StartDate")String StartDate,@RequestParam(value="SelectTime")String SelectTime,
                              @RequestParam(value="StartTime")String StartTime,@RequestParam(value="ServWorkLength")String ServWorkLength,
                              @RequestParam(value="StaffYbid")String StaffYbid,@RequestParam(value="ChosenWeekday")List<String> ChosenWeekday,
                              @RequestParam(value="ArrangeID")String ArrangeID) throws ParseException {
        System.out.println("开始时间："+StartDate);
        String DueDate=StartDate;
        StringBuilder stringBuffer=new StringBuilder(StartTime);
        int iFlag=-1;
        do {
            iFlag = stringBuffer.indexOf(":");
            if (iFlag != -1) {
                stringBuffer.deleteCharAt(iFlag);
            }
        } while (iFlag != -1);
        String starttime=stringBuffer.toString();
        if(SelectTime.equals("长期不变")){
            System.out.println("进入长期不变");
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            String str = StartDate;
            str=str.replace("-","");
            Date dt = sdf.parse(str);
            System.out.println("进入长期不变"+dt);
            Calendar rightNow = Calendar.getInstance();
            rightNow.setTime(dt);
            // 日期加10天
            rightNow.add(Calendar.YEAR, 2);
            System.out.println("结束时间："+sdf.format(rightNow.getTime()));
            DueDate=sdf.format(rightNow.getTime());
            System.out.println("结束时间1"+DueDate);
            try{
                if(StartDate!="" && DueDate!="" && StartTime!="" && ServWorkLength!="" && StaffYbid!="" && ArrangeID!="" && ChosenWeekday.size()!=0){
                    System.out.println("准备查询");
                    List<Staff> findout = staffService.searchStaffbyYbid(StaffYbid);
                    System.out.println("查询护工是否存在");
                    if(!findout.isEmpty()) {
                        System.out.println("准备查询护工所有时间");
                        List<ArrangeInfo> find = arrangeInfoDao.searchStaffbyYbid(StaffYbid);
                        System.out.println("查询护工所有时间");
                        if(!find.isEmpty()){
                            for(int i=0;i<find.size();i++){
                                ArrangeInfo arrangeInfo=find.get(i);
                                Date serv_due_date=arrangeInfo.getServ_due_date();
                                String status=arrangeInfo.getStatus();
                                SimpleDateFormat formatter= new SimpleDateFormat("yyyyMMdd");
                                Date date = new Date(System.currentTimeMillis());
                                String current=formatter.format(date);
                                Date st = sdf.parse(current);
                                System.out.println("护工状态："+status);
                                if(status.equals("有效")){
                                    if(serv_due_date.before(st)){
                                        int id=arrangeInfo.getId();
                                        System.out.println("更改过期护工信息"+id);
                                        int e=arrangeInfoDao.updateOverTime(id,status);
                                    }
                                    else{
                                        StringBuffer sb=new StringBuffer();
                                        for (String str1:ChosenWeekday) {
                                            sb.append(str1+",");
                                        }
                                        sb.deleteCharAt(sb.lastIndexOf(","));
                                        String ChosenWeekdayToString=sb.toString();
                                        for (String str1:ChosenWeekday) {
                                            System.out.println("输出每个选中的工作日：" + str1);
                                            String chosenDay = null;
                                            switch (str1) {
                                                case "周一":
                                                    System.out.println("输出每个选中的工作日1：" + arrangeInfo.getServ_monday());
                                                    if (arrangeInfo.getServ_monday()==null||arrangeInfo.getServ_monday().equals("")){
                                                        System.out.println("周一是否为空");
                                                        break;
                                                    }
                                                    else{
                                                        stringBuffer = new StringBuilder(arrangeInfo.getServ_monday());
                                                        System.out.println("周四是否为空1111:"+stringBuffer);
                                                        do {
                                                            iFlag = stringBuffer.indexOf(":");
                                                            if (iFlag != -1) {
                                                                stringBuffer.deleteCharAt(iFlag);
                                                            }
                                                        } while (iFlag != -1);
                                                        String stringbuffer=stringBuffer.toString();
                                                        String[] strsplit1 = stringbuffer.split("~");
                                                        int start1=Integer.parseInt(strsplit1[0]);
                                                        System.out.println("周四是否为空2:"+start1);

                                                        int end1=Integer.parseInt(strsplit1[1]);
                                                        System.out.println("周四是否为空3:"+end1);

                                                        int Start1=Integer.parseInt(starttime);
                                                        int End1=Integer.parseInt(ServWorkLength);
                                                        System.out.println("周四是否为空4:"+End1);

                                                        End1=Start1*100+End1*10000;
                                                        if(End1<=start1||Start1>=end1){
                                                            System.out.println("周四是否为空4:");
                                                            break;
                                                        }
                                                        else{
                                                            return "{\"msg\":\"config\"}";
                                                        }
                                                    }
                                                case "周二":
                                                    if(arrangeInfo.getServ_tuesday()==null||arrangeInfo.getServ_tuesday().equals(""))
                                                        break;
                                                    else{
                                                         stringBuffer = new StringBuilder(arrangeInfo.getServ_tuesday());
                                                        //System.out.println("周四是否为空1111:"+stringBuffer);
                                                        do {
                                                            iFlag = stringBuffer.indexOf(":");
                                                            if (iFlag != -1) {
                                                                stringBuffer.deleteCharAt(iFlag);
                                                            }
                                                        } while (iFlag != -1);
                                                        String stringbuffer=stringBuffer.toString();
//                                                        stringBuffer=new StringBuilder(StartTime);
//                                                        do {
//                                                            iFlag = stringBuffer.indexOf(":");
//                                                            if (iFlag != -1) {
//                                                                stringBuffer.deleteCharAt(iFlag);
//                                                            }
//                                                        } while (iFlag != -1);
//                                                        StartTime=stringBuffer.toString();
                                                        String[] strsplit2 = stringbuffer.split("~");
                                                        int start2=Integer.parseInt(strsplit2[0]);
                                                        int end2=Integer.parseInt(strsplit2[1]);
                                                        int Start2=Integer.parseInt(starttime);
                                                        int End2=Integer.parseInt(ServWorkLength);
                                                        End2=Start2*100+End2*10000;
                                                        if(End2<=start2||Start2>=end2){
                                                            break;
                                                        }
                                                        else{
                                                            return "{\"msg\":\"config\"}";
                                                        }
                                                    }
                                                case "周三":
                                                    if(arrangeInfo.getServ_wednesday()==null||arrangeInfo.getServ_wednesday().equals(""))
                                                        break;
                                                    else{
                                                        stringBuffer = new StringBuilder(arrangeInfo.getServ_wednesday());
                                                        //System.out.println("周四是否为空1111:"+stringBuffer);
                                                        do {
                                                            iFlag = stringBuffer.indexOf(":");
                                                            if (iFlag != -1) {
                                                                stringBuffer.deleteCharAt(iFlag);
                                                            }
                                                        } while (iFlag != -1);
                                                        String stringbuffer=stringBuffer.toString();
//                                                        stringBuffer=new StringBuilder(StartTime);
//                                                        do {
//                                                            iFlag = stringBuffer.indexOf(":");
//                                                            if (iFlag != -1) {
//                                                                stringBuffer.deleteCharAt(iFlag);
//                                                            }
//                                                        } while (iFlag != -1);
//                                                        StartTime=stringBuffer.toString();
                                                        String[] strsplit3 = stringbuffer.split("~");
                                                        int start3=Integer.parseInt(strsplit3[0]);
                                                        int end3=Integer.parseInt(strsplit3[1]);
                                                        int Start3=Integer.parseInt(starttime);
                                                        int End3=Integer.parseInt(ServWorkLength);
                                                        End3=Start3*100+End3*10000;
                                                        if(End3<=start3||Start3>=end3){
                                                            break;
                                                        }
                                                        else{
                                                            return "{\"msg\":\"config\"}";
                                                        }
                                                    }
                                                case "周四":
                                                    if(arrangeInfo.getServ_thursday()==null||arrangeInfo.getServ_thursday().equals("")){
                                                        System.out.println("周一是否为空");
                                                        break;
                                                    }
                                                    else{
                                                        //System.out.println("周四是否为空arrangeInfo.getServ_thursday()::"+arrangeInfo.getServ_thursday());
                                                        stringBuffer = new StringBuilder(arrangeInfo.getServ_thursday());
                                                        //System.out.println("周四是否为空1111:"+stringBuffer);
                                                        do {
                                                            iFlag = stringBuffer.indexOf(":");
                                                            if (iFlag != -1) {
                                                                stringBuffer.deleteCharAt(iFlag);
                                                            }
                                                        } while (iFlag != -1);
                                                        String stringbuffer=stringBuffer.toString();
                                                        stringBuffer=new StringBuilder(starttime);
                                                        do {
                                                            iFlag = stringBuffer.indexOf(":");
                                                            if (iFlag != -1) {
                                                                stringBuffer.deleteCharAt(iFlag);
                                                            }
                                                        } while (iFlag != -1);
                                                        StartTime=stringBuffer.toString();
                                                        //System.out.println("周四是否为空"+stringbuffer);
                                                        //System.out.println("周四是否为空1"+StartTime);
                                                        //System.out.println("周四是否为空1:"+ServWorkLength);
                                                        String[] strsplit4 = stringbuffer.split("~");
                                                        //System.out.println("strsplit4:"+strsplit4[0]);
                                                        int start4=Integer.parseInt(strsplit4[0]);

                                                        //System.out.println("start4:"+start4);
                                                        int end4=Integer.parseInt(strsplit4[1]);

                                                        //System.out.println("end4:"+end4);
                                                        int Start4=Integer.parseInt(StartTime);

                                                        //System.out.println("Start4:"+Start4);
                                                        int End4=Integer.parseInt(ServWorkLength);

                                                        //System.out.println("End4:"+End4);
                                                        End4=Start4*100+End4*10000;
                                                        //System.out.println("周四是否为空"+End4);
                                                        if(End4<=start4||Start4>=end4){
                                                            break;
                                                        }
                                                        else{
                                                            //System.out.println("周四是否为空111");
                                                            return "{\"msg\":\"config\"}";
                                                        }
                                                    }
                                                case "周五":
                                                    if(arrangeInfo.getServ_friday()==null||arrangeInfo.getServ_friday().equals(""))
                                                        break;
                                                    else{
                                                        stringBuffer = new StringBuilder(arrangeInfo.getServ_friday());
                                                        //System.out.println("周四是否为空1111:"+stringBuffer);

                                                        do {
                                                            iFlag = stringBuffer.indexOf(":");
                                                            if (iFlag != -1) {
                                                                stringBuffer.deleteCharAt(iFlag);
                                                            }
                                                        } while (iFlag != -1);
                                                        String stringbuffer=stringBuffer.toString();
                                                        stringBuffer=new StringBuilder(StartTime);
//                                                        do {
//                                                            iFlag = stringBuffer.indexOf(":");
//                                                            if (iFlag != -1) {
//                                                                stringBuffer.deleteCharAt(iFlag);
//                                                            }
//                                                        } while (iFlag != -1);
//                                                        StartTime=stringBuffer.toString();
                                                        String[] strsplit5= stringbuffer.split("~");
                                                        int start5=Integer.parseInt(strsplit5[0]);
                                                        int end5=Integer.parseInt(strsplit5[1]);
                                                        int Start5=Integer.parseInt(starttime);
                                                        int End5=Integer.parseInt(ServWorkLength);
                                                        End5=Start5*100+End5*10000;
                                                        if(End5<=start5||Start5>=end5){
                                                            break;
                                                        }
                                                        else{
                                                            return "{\"msg\":\"config\"}";
                                                        }
                                                    }
                                                case "周六":
                                                    if(arrangeInfo.getServ_saturday()==null||arrangeInfo.getServ_saturday().equals(""))
                                                        break;
                                                    else{
                                                        stringBuffer = new StringBuilder(arrangeInfo.getServ_saturday());
                                                        //System.out.println("周四是否为空1111:"+stringBuffer);
                                                        do {
                                                            iFlag = stringBuffer.indexOf(":");
                                                            if (iFlag != -1) {
                                                                stringBuffer.deleteCharAt(iFlag);
                                                            }
                                                        } while (iFlag != -1);
                                                        String stringbuffer=stringBuffer.toString();
//                                                        stringBuffer=new StringBuilder(StartTime);
//                                                        do {
//                                                            iFlag = stringBuffer.indexOf(":");
//                                                            if (iFlag != -1) {
//                                                                stringBuffer.deleteCharAt(iFlag);
//                                                            }
//                                                        } while (iFlag != -1);
//                                                        StartTime=stringBuffer.toString();
                                                        String[] strsplit6 = stringbuffer.split("~");
                                                        int start6=Integer.parseInt(strsplit6[0]);
                                                        int end6=Integer.parseInt(strsplit6[1]);
                                                        int Start6=Integer.parseInt(starttime);
                                                        int End6=Integer.parseInt(ServWorkLength);
                                                        End6=Start6*100+End6*10000;
                                                        if(End6<=start6||Start6>=end6){
                                                            break;
                                                        }
                                                        else{
                                                            return "{\"msg\":\"config\"}";
                                                        }
                                                    }
                                                case "周日":
                                                    if(arrangeInfo.getServ_sunday()==null||arrangeInfo.getServ_sunday().equals(""))
                                                        break;
                                                    else{
                                                        stringBuffer = new StringBuilder(arrangeInfo.getServ_sunday());
                                                        //System.out.println("周四是否为空1111:"+stringBuffer);
                                                        do {
                                                            iFlag = stringBuffer.indexOf(":");
                                                            if (iFlag != -1) {
                                                                stringBuffer.deleteCharAt(iFlag);
                                                            }
                                                        } while (iFlag != -1);
                                                        String stringbuffer=stringBuffer.toString();
//                                                        stringBuffer=new StringBuilder(StartTime);
//                                                        do {
//                                                            iFlag = stringBuffer.indexOf(":");
//                                                            if (iFlag != -1) {
//                                                                stringBuffer.deleteCharAt(iFlag);
//                                                            }
//                                                        } while (iFlag != -1);
//                                                        StartTime=stringBuffer.toString();
                                                        String[] strsplit7 = stringbuffer.split("~");
                                                        int start2=Integer.parseInt(strsplit7[0]);
                                                        int end2=Integer.parseInt(strsplit7[1]);
                                                        int Start2=Integer.parseInt(starttime);
                                                        int End2=Integer.parseInt(ServWorkLength);
                                                        End2=Start2*100+End2*10000;
                                                        if(End2<=start2||Start2>=end2){
                                                            break;
                                                        }
                                                        else{
                                                            return "{\"msg\":\"config\"}";
                                                        }
                                                    }
                                            }
                                        }
                                    }
                                }
                            }
                            System.out.println("护工不存在冲突");
                            String serv_status="长期不变";
                            String ServTime = arrangeInfoService.choseServTime(StartDate, DueDate, StartTime, ServWorkLength, StaffYbid, ChosenWeekday, ArrangeID,serv_status);
                            System.out.println("完成时间排程1");
                            return "{\"msg\":\"SuccessfullyChoseServTime\"}";
                        }
                        else{
                            System.out.println("护工不存在冲突");
                            String serv_status="长期不变";
                            String ServTime = arrangeInfoService.choseServTime(StartDate, DueDate, StartTime, ServWorkLength, StaffYbid, ChosenWeekday, ArrangeID,serv_status);
                            System.out.println("完成时间排程");
                            return "{\"msg\":\"SuccessfullyChoseServTime\"}";
                        }
                    }else{
                        System.out.println("controller层：不存在该护工");
                        return "{\"msg\":\"noStaff\"}";
                    }
                }else{
                    System.out.println("数据不完整");
                    return "{\"msg\":\"IncompleteData\"}";
                }
            }
            catch (Exception e) {
                e.printStackTrace();
                logger.error("更新异常！", e);
                System.out.println("操作异常，请您稍后再试");
            }
        }
        else if(SelectTime.equals("长期变更")){
            System.out.println("进入到长期变更");
            ArrangeInfo newStaff=arrangeInfoDao.searchStaffRecord(ArrangeID);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            Date enddate=newStaff.getServ_due_date();

            String str = StartDate;
            str=str.replace("-","");
            Date dt = sdf.parse(str);
            DueDate=sdf.format(enddate);
            System.out.println("开始时间："+StartDate);
            System.out.println("结束时间："+DueDate);
            if(enddate.before(dt)){
                return "{\"msg\":\"IncompleteData\"}";
            }
            System.out.println("长期变更："+newStaff.getServ_start_date());
            List<Staff> findout1 = staffService.searchStaffbyYbid(StaffYbid);
            System.out.println("查询护工是否存在:"+findout1.size());
            if(!findout1.isEmpty()){
                System.out.println("查询护工是否存在1:"+findout1.size());
                List<ArrangeInfo> find1 = arrangeInfoDao.searchStaffbyYbid(StaffYbid);
                System.out.println("查询护工是否存在2:"+find1.size());
                if(!find1.isEmpty()){
                    String s="1";
                    for(int i=0;i<find1.size();i++){
                        ArrangeInfo arrangeInfo=find1.get(i);
                        int arrangeID=Integer.parseInt(ArrangeID);
                        if(arrangeInfo.getId()==arrangeID){
                            System.out.println("当前查询的时间为变更记录的时间");
                            continue;
                        }
                        String[] arr1=new String[]{"周一","周二","周三","周四","周五","周六","周日"};
                        String[] arr=new String[]{arrangeInfo.getServ_monday(),arrangeInfo.getServ_tuesday(),arrangeInfo.getServ_wednesday(),arrangeInfo.getServ_thursday(),
                        arrangeInfo.getServ_friday(),arrangeInfo.getServ_saturday(),arrangeInfo.getServ_sunday()};
                        StringBuffer sb=new StringBuffer();
                        for (String str1:ChosenWeekday) {
                            sb.append(str1+",");
                        }
                        sb.deleteCharAt(sb.lastIndexOf(","));
                        String ChosenWeekdayToString=sb.toString();
                        for(int j=0;j<arr.length;j++){
                             //s=arr[j];
                             if(arr[j]==null||arr[j].equals("")){
                                 continue;
                             }
                            else{
                                 for(String str1:ChosenWeekday){
                                     System.out.println("输出每个选中的工作日：" + str1);
                                     if(arr1[j].equals(str1)){
                                         stringBuffer = new StringBuilder(arr[j]);
                                         do {
                                             iFlag = stringBuffer.indexOf(":");
                                             if (iFlag != -1) {
                                                 stringBuffer.deleteCharAt(iFlag);
                                             }
                                         } while (iFlag != -1);
                                         String stringbuffer=stringBuffer.toString();
                                         String[] strsplit1 = stringbuffer.split("~");
                                         int start1=Integer.parseInt(strsplit1[0]);
                                         System.out.println("周四是否为空2:"+start1);

                                         int end1=Integer.parseInt(strsplit1[1]);
                                         System.out.println("周四是否为空3:"+end1);

                                         int Start1=Integer.parseInt(starttime);
                                         int End1=Integer.parseInt(ServWorkLength);
                                         System.out.println("周四是否为空4:"+End1);

                                         End1=Start1*100+End1*10000;
                                         if(End1<=start1||Start1>=end1){
                                             System.out.println("周四是否为空4:");
                                             break;
                                         }
                                         else{
                                             return "{\"msg\":\"config\"}";
                                         }
                                     }
                                 }
                             }
                        }

                    }
                    System.out.println("护工存在数据3："+StartDate);
                    String serv_status="长期变更";
                    String ServTime = arrangeInfoService.choseServTime(StartDate, DueDate, StartTime, ServWorkLength, StaffYbid, ChosenWeekday, ArrangeID,serv_status);
                    System.out.println("护工存在数据4："+StartDate);
                    return "{\"msg\":\"SuccessfullyChange\"}";
                }
                else{
                    System.out.println("护工不存在冲突");
                    String serv_status="长期变更";
                    String ServTime = arrangeInfoService.choseServTime(StartDate, DueDate, StartTime, ServWorkLength, StaffYbid, ChosenWeekday, ArrangeID,serv_status);
                    System.out.println("完成时间排程");
                    return "{\"msg\":\"SuccessfullyChange\"}";
                }
            }
            else{
                System.out.println("controller层：不存在该护工");
                return "{\"msg\":\"noStaff\"}";
            }
        }
        else if(SelectTime.equals("短期变更")){
            System.out.println("进入短期变更");
            int c=arrangeInfoDao.ChangeShortRecord(ArrangeID);
            System.out.println("变更成功");
            ArrangeInfo newStaff=arrangeInfoDao.searchStaffRecord(ArrangeID);
            List<ArrangeInfo> find1 = arrangeInfoDao.searchStaffbyYbid(StaffYbid);
            System.out.println("查询护工是否存在2:"+find1.size());
            if(!find1.isEmpty()) {
                for (int i = 0; i < find1.size(); i++) {
                    ArrangeInfo arrangeInfo = find1.get(i);
                    int arrangeID = Integer.parseInt(ArrangeID);
                    if (arrangeInfo.getId() == arrangeID) {
                        System.out.println("当前查询的时间为变更记录的时间");
                        continue;
                    }
                    String[] arr1 = new String[]{"周一", "周二", "周三", "周四", "周五", "周六", "周日"};
                    String[] arr = new String[]{arrangeInfo.getServ_monday(), arrangeInfo.getServ_tuesday(), arrangeInfo.getServ_wednesday(), arrangeInfo.getServ_thursday(),
                            arrangeInfo.getServ_friday(), arrangeInfo.getServ_saturday(), arrangeInfo.getServ_sunday()};
                    StringBuffer sb = new StringBuffer();
                    for (String str1 : ChosenWeekday) {
                        sb.append(str1 + ",");
                    }
                    sb.deleteCharAt(sb.lastIndexOf(","));
                    String ChosenWeekdayToString = sb.toString();
                    for (int j = 0; j < arr.length; j++) {
                        //s=arr[j];
                        if (arr[j] == null || arr[j].equals("")) {
                            continue;
                        } else {
                            for (String str1 : ChosenWeekday) {
                                System.out.println("输出每个选中的工作日：" + str1);
                                if (arr1[j].equals(str1)) {
                                    stringBuffer = new StringBuilder(arr[j]);
                                    do {
                                        iFlag = stringBuffer.indexOf(":");
                                        if (iFlag != -1) {
                                            stringBuffer.deleteCharAt(iFlag);
                                        }
                                    } while (iFlag != -1);
                                    String stringbuffer = stringBuffer.toString();
                                    String[] strsplit1 = stringbuffer.split("~");
                                    int start1 = Integer.parseInt(strsplit1[0]);
                                    System.out.println("是否为空2:" + start1);

                                    int end1 = Integer.parseInt(strsplit1[1]);
                                    System.out.println("是否为空3:" + end1);

                                    int Start1 = Integer.parseInt(starttime);
                                    System.out.println("是否为空4:" + Start1);
                                    int End1 = Integer.parseInt(ServWorkLength);
                                    System.out.println("是否为空4:" + End1);
                                    Start1=Start1*100;
                                    End1 = Start1  + End1 * 10000;
                                    System.out.println("是否为空4:" + End1);
                                    if (End1 <= start1 || Start1 >= end1) {
                                        System.out.println("是否为空4:");
                                        break;
                                    } else {
                                        return "{\"msg\":\"config\"}";
                                    }
                                }
                            }
                        }
                    }
                }
            }

            //ArrangeInfo arrangeInfo=newStaff;
            ArrangeInfo newStaff1=arrangeInfoDao.searchStaffRecord(ArrangeID);
            int id=Integer.parseInt(ArrangeID);
            String  elderYbid=newStaff1.getElder_yb_id().toString();
            String OrgSettleCode=newStaff.getSettle_code();
            String servContentToString=newStaff1.getServ_content();
            Date arrange_time=newStaff1.getArrange_time();
            String  status=newStaff1.getStatus();
            String servIDToString=newStaff1.getServ_ID();
            System.out.println("111111111:"+status);
            try {
                int n=arrangeInfoDao.insertAllShort(id,elderYbid,OrgSettleCode,servContentToString,arrange_time,status,servIDToString);
                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
                String str = StartDate;
                str=str.replace("-","");
                Date dt = sdf.parse(str);
                System.out.println("进入长期不变"+dt);
                Calendar rightNow = Calendar.getInstance();
                rightNow.setTime(dt);
                // 日期加10天
                rightNow.add(Calendar.DAY_OF_MONTH, 7);
                System.out.println("结束时间："+sdf.format(rightNow.getTime()));
                DueDate=sdf.format(rightNow.getTime());
                String serv_status="短期变更";
                int ServShortTime = arrangeInfoService.choseServShortTime(StartDate, DueDate, StartTime, ServWorkLength, StaffYbid, ChosenWeekday, ArrangeID,serv_status);
                return "{\"msg\":\"SuccessfullyShortChange\"}";
            } catch (Exception e) {
                e.printStackTrace();
                logger.error("更新异常！", e);
                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
                String str = StartDate;
                str=str.replace("-","");
                Date dt = sdf.parse(str);
                System.out.println("进入长期不变"+dt);
                Calendar rightNow = Calendar.getInstance();
                rightNow.setTime(dt);
                // 日期加10天
                rightNow.add(Calendar.DAY_OF_MONTH, 7);
                System.out.println("结束时间："+sdf.format(rightNow.getTime()));
                DueDate=sdf.format(rightNow.getTime());
                String serv_status="短期变更";
                int ServShortTime = arrangeInfoService.choseServShortTime(StartDate, DueDate, StartTime, ServWorkLength, StaffYbid, ChosenWeekday, ArrangeID,serv_status);
                return "{\"msg\":\"SuccessfullyShortChange\"}";
            }

            //arrangeInfo
        }
        else{
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            String str = StartDate;
            str=str.replace("-","");
            Date dt = sdf.parse(str);
            Calendar rightNow = Calendar.getInstance();
            rightNow.setTime(dt);
            // 日期加10天
            rightNow.add(Calendar.DAY_OF_YEAR, 1);
            System.out.println("结束时间："+sdf.format(rightNow.getTime()));
            DueDate=sdf.format(rightNow.getTime());
        }
        System.out.println("进入controller层的choseServTime");
        System.out.println("开始日期："+StartDate+","+"结束日期："+DueDate);
        System.out.println("开始时间："+StartTime+","+"工作时长："+ServWorkLength);
        System.out.println("护士医保："+StaffYbid+","+"选择工作日："+ChosenWeekday);
        System.out.println("订单制定时间："+ArrangeID);
        return "{\"msg\":\"UnsuccessfullyChoseServTime\"}";
    }

    /**
     * 派工日程安排页面：搜索护工
     * @param NursingName
     * @param NursingYbid
     * @param NursingID
     * @return
     */
    @RequestMapping("/searchArrangeNursing")
    @ResponseBody
    public PageInfo searchArrangeNursing(HttpServletRequest request,@RequestParam(value="pn",defaultValue = "1")Integer pn,
                                         @RequestParam(value="limit",defaultValue="10")Integer limit,
                                         @RequestParam(value = "NursingName") String NursingName ,
                                         @RequestParam(value="NursingYbid")String NursingYbid,
                                         @RequestParam(value="NursingID")String NursingID) {
        System.out.println("进入controller层");
        User user = (User) request.getSession().getAttribute(ConstantsUtils.SessionObj.CURRENT_USER);
        String Role_id=this.userService.findRoleIDbyUserNumber(user.getUser_number());
        if (NursingName == "" && NursingYbid =="" && NursingID=="") {
            System.out.println("搜索框无数据");
            if(Role_id.equals("2")){
                String SettleCode =userService.findSettleCodebyUserNumber(user.getUser_number());
                PageHelper.startPage(pn, limit);
                List<Staff> staffList=staffService.getStaffbySettleCode(SettleCode);
                PageInfo page = new PageInfo(staffList);
                return page;
            }else{
                PageHelper.startPage(pn, limit);
                List<Staff> staffList = staffService.getAllStaffList();
                PageInfo page = new PageInfo(staffList);
                return page;
            }
        } else{
            if(Role_id.equals("6")){
                String SettleCode =userService.findSettleCodebyUserNumber(user.getUser_number());
                PageHelper.startPage(pn, limit);
                List<Staff> staffList=staffService.searchArrangeNursingbySettelCode(NursingName,NursingYbid,NursingID,SettleCode);
                PageInfo page = new PageInfo(staffList);
                return page;
            }else{
                PageHelper.startPage(pn, limit);
                List<Staff> staffList = staffService.searchArrangeNursing(NursingName,NursingYbid,NursingID);
                PageInfo page = new PageInfo(staffList);
                return page;
            }
        }
    }

    /**
     * 派工日程安排页面：搜索护工的派工日程
     * @param NursingYbid
     * @param whichWeek
     * @return
     */
    @RequestMapping("/findNurseArrangement")
    @ResponseBody
    public List<Map<String,Object>> findNurseArrangement(@RequestParam(value="NursingYbid")String NursingYbid,
                                                         @RequestParam(value="whichWeek")String whichWeek){
        System.out.println("controller层：护工医保ID："+NursingYbid);
        System.out.println("controller层：哪一周："+whichWeek);
        List<Map<String,Object>> findNurseArrangement=arrangeInfoService.findNurseArrangement(NursingYbid,whichWeek);
        System.out.println("输出获取值："+findNurseArrangement);
        return findNurseArrangement;
    }

    /**
     * 派工单信息页面：删除订单
     * @param id
     */
    @RequestMapping(value = "/deleteArrangeOrder")
    @ResponseBody
    public String deleteArrangeOrder(@RequestParam("id") int id) {
        try {
            if (id > 0) {
                return arrangeInfoService.deleteArrangeOrder(id);
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("删除护工异常！", e);
        }
        return "删除机构出错，请您稍后再试";
    }

    /**
     * 派工单信息页面：删除订单
     */
    @RequestMapping(value = "/judgeStatus")
    @ResponseBody
    public String judgeStatus(HttpServletRequest request, HttpServletResponse response, HttpSession session){
        System.out.println("进入controller层：judgeStatus");
        try{
            this.arrangeInfoService.judgeStatus();
            return "ok";
        }catch (Exception e) {
            e.printStackTrace();
            logger.error("删除护工异常！", e);
        }
        return "删除机构出错，请您稍后再试";
    }

    /**
     * 微信小程序：登录页面-搜索护工信息
     * @param StaffYbid
     * @param password
     * @return
     */
    @RequestMapping("/wechatLoginStaff")
    @ResponseBody
    public String wechatLoginStaff(@RequestParam(value = "StaffYbid") String StaffYbid,
                                   @RequestParam(value="password")String password) {
        System.out.println("controller层："+StaffYbid);
        System.out.println("controller层："+password);
        try{
            if (StaffYbid == "" || password =="") {
                System.out.println("controller层：登录框数据不全");
                return "noContent";
            }
            else{
                System.out.println("contoller层：搜索框非空");
                String login=arrangeInfoService.wechatLoginStaff(StaffYbid,password);
                System.out.println("controller层：搜索成功");
                System.out.println("controller层：输出login"+login);
                switch (login){
                    case "correct":return "CorrectLogin";
                    case "errorPassword":return "errorPassword";
                    case "noStaff":return "noStaff";
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("删除护工异常！", e);
        }
        return "error";
    }

//    /**
//     * 微信小程序：老人页面：查询到护工的服务老人
//     * @param StaffYbid
//     * @return
//     */
//    @RequestMapping("/wechatElderBySatff")
//    @ResponseBody
//    public List<Map<String,Object>> wechatElderBySatff(@RequestParam(value="StaffYbid")String StaffYbid){
//        System.out.println("controller层：护工医保ID："+StaffYbid);
//        List<Map<String,Object>> wechatElderBySatff=arrangeInfoService.wechatElderBySatff(StaffYbid);
//        System.out.println("输出获取值："+wechatElderBySatff);
//        return wechatElderBySatff;
//    }


    /**
     * 微信小程序：老人页面：根据护工的id查到服务的老人
     * @param nursing_yb_id
     * @return
     */
    @RequestMapping("/getMyelder")
    @ResponseBody
    public List<ArrangeInfo> getMyelder( HttpServletRequest request,@RequestParam(value="nursing_yb_id")String nursing_yb_id){
        System.out.println("controller层：护工医保ID："+ nursing_yb_id);
        ArrangeInfo arrangeInfo = new ArrangeInfo();
        arrangeInfo.setNursing_yb_id(Integer.parseInt(nursing_yb_id));
        List<ArrangeInfo> getMyelder=arrangeInfoDao.getMyelder(arrangeInfo);
        System.out.println("输出获取值："+getMyelder);
        return getMyelder;
    }


    /**
     * 微信小程序：健康信息页面：查询到老人的服务内容
     * @param ArrangeID
     * @return
     */
    @RequestMapping("/wechatServIDByID")
    @ResponseBody
    public List<String> wechatServIDByID(@RequestParam(value="ArrangeID")String ArrangeID){
        System.out.println("controller层：订单ID："+ArrangeID);
        List<String> wechatServIDByID=arrangeInfoService.wechatServIDByID(ArrangeID);
        System.out.println("controller层：输出获取值："+wechatServIDByID);
        return wechatServIDByID;
    }

    /**
     * 根据HgId获取派工单
     * @param arrangeInfo
     * @return
     */
    @RequestMapping("/getArrangeByHgId")
    @ResponseBody
    public List<ArrangeInfo> getArrangeByHgId(ArrangeInfo arrangeInfo){
        System.out.println("Controller层：根据HgId获取派工单");
        List<ArrangeInfo> list = arrangeInfoService.getArrangeByHgId(arrangeInfo);
        return list;
    }
}



